package com.imu.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.xiaoleilu.hutool.date.DatePattern;
import com.xiaoleilu.hutool.date.DateTime;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * 日期处理
 * @author sunhongwei
 * @email sunhongwei@gmail.com
 * @date 2017年11月4日 上午11:07:35
 */
public class DateUtils
{
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	/** 时间格式(yyyyMMddHHmmss) */
	public final static String DATE_TIME = "yyyyMMddHH";
	
	public static String now()
	{
        return format(new Date(), DATE_TIME);
    }
	
	public static String format(Date date)
	{
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern)
    {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }
    
    /**
     * 把日期字符串格式化成日期类型
     *    时间System.currentTimeMillis() 毫秒 格式
     * @param dateStr
     * @param format
     * @return
     */
    public static String long2Str(long ts)
    {
        SimpleDateFormat simple = new SimpleDateFormat(DATE_TIME_PATTERN);
        try
        {
        	Calendar cal = Calendar.getInstance();
        	cal.setTimeInMillis(ts);
            return simple.format(cal.getTime());
        } 
        catch (Exception e)
        {  
            return  null;  
        }  
     }
    
    /**
     * 获取当前日期的 前后日期
     * @return
     */
    public static long startDay(int n)
    {
    	Calendar cale = Calendar.getInstance();
    	cale.add(Calendar.DATE, n);
    	cale.set(Calendar.HOUR_OF_DAY, 0);
    	cale.set(Calendar.MINUTE, 0);
    	cale.set(Calendar.SECOND, 0);
    	cale.set(Calendar.MILLISECOND, 0);
        
        return cale.getTime().getTime();
     }
    
    /**
     * 获取当前日期的 前后日期
     * @return
     */
	public static long endDay(int n)
    {
    	Calendar cale = Calendar.getInstance();  
    	cale.add(Calendar.DATE, n);
    	cale.set(Calendar.HOUR_OF_DAY, 23);
    	cale.set(Calendar.MINUTE, 59);
    	cale.set(Calendar.SECOND, 59);
    	cale.set(Calendar.MILLISECOND, 999);
        return cale.getTime().getTime();
	}
    
    /**
     * 获取当前月 第一天
     * @return
     */
    public static long startMonth()
    {
    	Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
    	cale.set(Calendar.HOUR_OF_DAY, 0);
    	cale.set(Calendar.MINUTE, 0);
    	cale.set(Calendar.SECOND, 0);
    	cale.set(Calendar.MILLISECOND, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        
        return cale.getTime().getTime();
	}
    
    /**
     * 获取当前月 第一天
     * @return
     */
    public static String sMonth()
    {
        SimpleDateFormat simple = new SimpleDateFormat(DATE_TIME_PATTERN);
    	Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
    	cale.set(Calendar.HOUR_OF_DAY, 0);
    	cale.set(Calendar.MINUTE, 0);
    	cale.set(Calendar.SECOND, 0);
    	cale.set(Calendar.MILLISECOND, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        
        return simple.format(cale.getTime());
     }
    
    /**
     * 获取当前月 最后一天
     * @return
     */
	public static long endMonth()
    {
    	Calendar cale = Calendar.getInstance();  
    	cale.add(Calendar.MONTH, 1);
    	cale.set(Calendar.HOUR_OF_DAY, 23);
    	cale.set(Calendar.MINUTE, 59);
    	cale.set(Calendar.SECOND, 59);
    	cale.set(Calendar.MILLISECOND, 999);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return cale.getTime().getTime();
	}
	
	/**
     * 获取当前月 最后一天
     * @return
     */
	public static String eMonth()
    {
        SimpleDateFormat simple = new SimpleDateFormat(DATE_TIME_PATTERN);
    	Calendar cale = Calendar.getInstance();  
    	cale.add(Calendar.MONTH, 1);
    	cale.set(Calendar.HOUR_OF_DAY, 23);
    	cale.set(Calendar.MINUTE, 59);
    	cale.set(Calendar.SECOND, 59);
    	cale.set(Calendar.MILLISECOND, 999);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return simple.format(cale.getTime());
	}
	
	public static List<String> lastList(int n)
	{
		List<String> list = new ArrayList<String>();
		
		for(int i = 1; i <= n; i++)
		{
			list.add(DateUtil.offsetDay(new DateTime(), i - n).toString(DatePattern.NORM_DATE_PATTERN));
		}
		
		return list;
	}
	
	public static void main(String[] args) 
	{
		List<String> list = lastList(30);
		System.out.println(list.size());
		for (String s : list) {
			System.out.println(s);
		}
	}
}
